package com.ybc.对于所有对象都通用的方法;


public class Human implements Comparable< Human> {

    private String name;
    private int age;

    public Human() {
        this.name = "ybc";
        this.age = 18;
    }

    public static void main(String[] args) {
        Human h1 = new Human();
        System.out.println(h1.equals(null));
        System.out.println(h1.equals(h1));
        System.out.println(h1.equals(new Human()));
        System.out.println(h1.equals(new Object()));
    }


    //入参必须是obj  必须先检查类型 null必须返回false
    @Override
    public boolean equals(Object obj) {
        if(obj == null){
            return false;
        }
        if(this == obj){
            return true;
        }
        if(!(obj instanceof Human)){
            return false;
        }
        return this.name.equals(((Human) obj).name) && this.age == ((Human) obj).age;

    }


    //有了equals方法后，必须重写hashCode方法
    @Override
    public int hashCode() {
        return 0;
    }


    //toString方法 提高可读性
    @Override
    public String toString() {
        return "Human{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }


    //类需要比较运算时 考虑实现比较接口 且用compareTo方法来比较
    @Override
    public int compareTo(Human o) {
        //先根据年龄 再根据name
        if(this.age != o.age){
            return Integer.compare(this.age, o.age);
        }
        return this.name.compareTo(o.name);


    }
}
